<template>
  <div class="bgi">
      <div class="login" >
      <h2 class="loginTitle">智慧停车后台管理</h2>
      <el-form ref="login" label-width="140px" :rules="rules" :model="user" >
        <el-form-item label="用户名" prop="username">
          <el-input autocomplete="off" v-model="user.username" clearable placeholder="3-5位数" ref="usernameBtn" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input type="password" autocomplete="off" v-model="user.pwd" clearable placeholder="4到8位字母下划线数字组成" ref="pwdBtn"/>
        </el-form-item>
        <el-form-item label="验证码" prop="verify" class="verifyBox">
          <el-input type="text" autocomplete="off" v-model="user.verify" clearable  class="verifyBtn" />
          <div>
          <img src="../../assets/imgs/verify.png" alt="" class="verifyImg">
          </div>
        </el-form-item>
        <el-form-item class="remember">
          <el-checkbox v-model="checked1" label="记住用户名和密码" size="large" />
          <p class="forgetPwd">忘记密码</p>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit(login)">提交</el-button>
          <el-button @click="clearValue" class="resetBtn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
const login = ref()
const usernameBtn = ref()
const pwdBtn = ref()
const checked1 = ref(localStorage.getItem('pwd')?true:false)
const user = reactive({
  username: '',
  pwd: checked1.value?localStorage.getItem('pwd'):'',
  verify:''
})
// 清空input
const clearValue = () => {
  user.username = ''
  user.pwd = ''
}
// 定义验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 5, message: '3-5位数', trigger: 'blur' },
  ],
  pwd: [
    { required: true, message: '请填写密码' },
    {
      pattern: /^\w{4,8}$/,
      message: '4到8位字母下划线数字组成',
    },
  ],
  verify: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    {  pattern: /cht7v/i,
      message: '验证码不正确',
    }
  ],
})
const router = useRouter()
// 登录验证
const onSubmit = async (formEl:any)=> {
  if (!formEl) return
  await formEl.validate((valid:any) => {
    if (valid) {
      ElMessage({
      message: '登录成功',
      type: 'success',
      duration: 1000,
      onClose: () => {
        router.push('/DashBoard')
      
      }
  })
    } else {
      ElMessage({
        message: '账号或密码不正确',
      type: 'error',
      duration: 1000,
      onClose: () => {
        clearValue()
      }
      })
    }
    if(checked1.value){
          // localStorage.setItem('user',usernameBtn.value)
          /* localStorage.setItem('pwd',user.pwd)
          localStorage.setItem('checked1',checked1.value) */
    }
  })
}
// 判断是否选中记住密码

onMounted(() => {

})
</script>

<style scoped lang="scss">
.bgi {
  background-image: url(../../assets/imgs/bgi.jpg);
  height: 100vh;
  background-size: cover;
  background-repeat: no-repeat;
  // overflow: hidden;
  position: relative;
  display: flex;
}

.login {
  margin: auto;
  border-radius: 5%;
  // margin: 100px auto;
  background-color: rgba(0, 0, 0, 0.4);
  color: white;
  padding-top: 20px;
}

.el-form {
  width: 400px;
  padding: 30px 50px 30px 0px;

  :deep .el-form-item__label {
    color: white
  }

}

.verifyBox{
  display: flex;
  align-items: center;
}
.verifyBtn{
  width: 110px;
}

.verifyImg{
  width: 100px;
  position: relative;
  top: 6px;
}

.loginTitle{
  text-align: center;
}

.resetBtn{
  position: relative;
}

.remember{
  color: skyblue;
}
.forgetPwd{
  margin-left: 15px;
}

.el-checkbox{
color:skyblue
}
</style>